/**
 *
 */
package edu.nps.ai.agent;

import java.util.List;
import java.util.Random;


/**
 * @author Timothy Riley
 */
public class LabOneAgent extends AbstractAgent {
	/**
	 * Holds the possible directions
	 */
	private static String[] direction = {"forward", "aft", "port", "starboard"};

	/**
	 * Initialize the environment
	 *
	 * @param host
	 *            The host name of the server
	 * @param port
	 *            The port the server is running one
	 * @param userName
	 *            The user name to create in the environment
	 * @param password
	 *            The password for the user created
	 * @param delay
	 *            The delay between commands
	 * @param logFile
	 *            The log file
	 */
	public LabOneAgent(final String host, final Integer port, final String userName, final String password, final Integer delay, final String logFile) {
		initialize(host, port, userName, password, delay, logFile);
	}

	/**
	 * Assume the environment has been initialized
	 * @see edu.nps.ai.agent.Agent#execute()
	 */
	public void execute() {
		while (true) {
			String attempt = randomDirection();
			send(attempt);
			List<String> output = listen();
			Boolean allowed = verify(output, "Your energy is now");
			if (allowed) {
				String location = null;
				for (String line : output) {
					if (line.contains("(")) {
						location = line;
						break;
					}
				}
				if (location != null) {
					send("say I am in the " + location);
				} else {
					send("say Successful Move");
				}

			}
		}
	}

	/**
	 * Helper method to choose a random direction
	 * @return The random direction
	 */
	private String randomDirection() {
		Random random = new Random();
		Integer index = random.nextInt(4);
		return direction[index];
	}

	/**
	 * Main method to execute the agent
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 6) {
			throw new IllegalArgumentException("Wrong number of command line arguments!");
		}
		String hostName = args[0];
		Integer port = Integer.parseInt(args[1]);
		String userName = args[2];
		String password = args[3];
		Integer delay = Integer.parseInt(args[4]);
		String logFile = args[5];
		LabOneAgent lab = new LabOneAgent(hostName, port, userName, password, delay, logFile);
		lab.execute();
	}
}
